<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header class="header">
      <div>
        <img src="@/assets/img/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登陆</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle_button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#3a3f4a" text-color="#fff" active-text-color="#3e95ef" 
                 unique-opened :collapse="isCollapse" :collapse-transition="false" router
                 :default-active="activePath">  
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id"> <!-- :index="item.id + ''" 表示每个一级菜单要有自己的独一的index（只接受字符串），使用item.id加上空字符串转化为字符串属性即可 --> 
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>  <!--  根据独一的id，动态绑定字体图标class （iconsObj中保存图标名称）-->
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" 
                          :key="subitem.id" @click="saveNavState('/' + subitem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return{ 
      menulist: [], // 左侧菜单数据
      iconsObj: {  // 一级菜单左侧图标，根据id进行动态绑定class
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      isCollapse: false,  // 菜单是否折叠 
      activePath: '',    // 被激活的链接地址
    }
  },
  created() {
    this.getMenuList()  // 获取菜单列表
    this.activePath = window.sessionStorage.getItem('activePath') // 将sessionStorage中的activePath取出赋值给data中的activePath
  },
  methods: {
    logout() {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取所有的左侧菜单
    async getMenuList(){
      const {data: res} = await this.$axios.get('menus')
      // console.log(res);
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 将当前激活状态的链接保存到sessionStorage中的activePath
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath; // 立即将当前activePath 赋值给data中的activePath
    }
  }
}
</script>

<style lang="less" scoped>
  .home_container{
    height: 100%;
  }
  .el-header{  // element-Ui的标签名可以直接作为类名使用
    background-color: #3e4447;
    height: 80px !important;  // 默认的el-header高度为60px
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div{    
      display: flex;
      align-items: center;
      span{
        margin-left: 15px;
      }
    }
  }
  .el-aside{
    background-color: #3a3f4a;
    transition: all 0.2s;
    .el-menu{
      border: none;
    }
  }
  .el-main{
    background-color: #d8dcdf;
  }

  .iconfont{
    margin-right: 8px;
  }
  .toggle_button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.3em;
    cursor: pointer;
  }
</style>